let token = sessionStorage.getItem("token");
$(function(){

    loadData2(1);
    $("#btn_group input").click(function () {
        let count_item = $(this).attr("count_item");
        if (count_item === "deputy_team") {
            $("#thead").html(" <th>序号</th>\n" +
          
                "                                        <th>委员团组（组）</th>\n" +
                "                                        <th>委员统计</th>\n");
            status2 = "deputy_team";
            loadData2(1)
        } else if (count_item === "sex") {
          
            $("#thead").html(" <th>序号</th>\n" +
         
                "                                        <th>性别</th>\n" +
                "                                        <th>委员统计</th>\n");
            status2 = "sex";
            loadData2(1)
        } else if (count_item === "nation") {
            $("#thead").html(" <th>序号</th>\n" +
                "                                        <th>民族</th>\n" +
                "                                        <th>委员统计</th>\n");
            status2 = "nation";
            loadData2(1)
        } else if (count_item === "age") {
            $("#thead").html(" <th>序号</th>\n" +
                "                                        <th>年龄段</th>\n" +
                "                                        <th>委员统计</th>\n");
            status2 = "age";
            loadData2(1)
        }
        else if (count_item === "edu") {
            $("#thead").html(" <th>序号</th>\n" +
                "                                        <th>学历</th>\n" +
                "                                        <th>委员统计</th>\n");
            status2 = "edu";
            loadData2(1)
        } else if (count_item === "degree") {
            $("#thead").html(" <th>序号</th>\n" +
                "                                        <th>学位</th>\n" +
                "                                        <th>委员统计</th>\n");
            status2 = "degree";
            loadData2(1)
        } else if (count_item === "party_group") {
            $("#thead").html(" <th>序号</th>\n" +
                "                                        <th>党派</th>\n" +
                "                                        <th>委员统计</th>\n");
            status2 = "party_group";
            loadData2(1)
        }
      
    });


    //分页
    let pageNum = "1";
    let pages = "1";
    $(".first").click(function () {
        pageNum = 1;
        loadData(pageNum);
        $(this).parents("li").addClass("am-active");
        $(this).parents("li").siblings().removeClass("am-active");
    });
    $(".last").click(function () {
        pageNum = pages;
        loadData(pageNum);
        $(this).parents("li").addClass("am-active");
        $(this).parents("li").siblings().removeClass("am-active");
    });
    $(".up").click(function () {
        if (pageNum === 1) {
            return false;
        } else {
            pageNum--;
            loadData(pageNum);
        }
        $(this).parents("li").addClass("am-active");
        $(this).parents("li").siblings().removeClass("am-active");
    });
    $(".down").click(function () {
        if (pageNum === pages) {
            return false;
        } else {
            pageNum++;
            loadData(pageNum);
        }
        $(this).parents("li").addClass("am-active");
        $(this).parents("li").siblings().removeClass("am-active");
    });
});
let status2 = "deputy_team";
function loadData2(pageNum) {

    $.ajax({
        url: testurl + "users/db_count",
        type: "post",
        headers: { "Authorization": "Bearer " + token },
        contentType: "application/json",
        data: JSON.stringify({
            "pageNum": pageNum,//页码
            "count_item": status2//status:建议状态统计
        }),
        success: function (data) {
            if (status2 === "deputy_team") {
                $("#thead").html(" <th>序号</th>\n" +
                    "           <th>委员团组（组）</th>\n" +
                    "            <th>建议统计</th>\n");
                strr = "";
                charts_value = [];
                $.each(data.obj, function (index, value) {
                    if(value.item_name === undefined){
                        value.item_name = "";
                    }
                    let value2 = {};
                    strr += " <tr class=\"gradeX\">\n" +
                        "                                        <td>" + (index + 1) + "</td>\n" +
                        "                                        <td>" + value.item_name + "</td>\n" +
                        "                                        <td>" + value.count + "</td>\n" +
                        "</tr>";
                    value2.value = value.count;
                    value2.name = value.item_name;
                    charts_value.push(value2)
                });

                $("#show14").html(strr);
               
            } else if (status2 === "sex") {
                strr = "";
                charts_value = [];
                $.each(data.obj, function (index, value) {
                    let value2 = {};
                    strr += " <tr class=\"gradeX\">\n" +
                        "                                        <td>" + (index + 1) + "</td>\n" +
                        "                                        <td>" + value.item_name + "</td>\n" +
                        "                                        <td>" + value.count + "</td>\n" +
                        "</tr>";
                    value2.value = value.count;
                    value2.name = value.item_name;
                    charts_value.push(value2)
                });

                $("#show14").html(strr);

            } else if (status2 === "nation") {
                strr = "";
                charts_value = [];
                $.each(data.obj, function (index, value) {
                    if (value.item_name === undefined) {
                        value.item_name = "";
                    }
                    let value2 = {};
                    strr += " <tr class=\"gradeX\">\n" +
                        "                                        <td>" + (index + 1) + "</td>\n" +
                        "                                        <td>" + value.item_name + "</td>\n" +
                        "                                        <td>" + value.count + "</td>\n" +
                        "</tr>";
                    value2.value = value.count;
                    value2.name = value.item_name;
                    charts_value.push(value2)
                });

                $("#show14").html(strr);

            } else if (status2 === "age") {
                
                strr = "";
                charts_value = [];
                $.each(data.obj, function (index, value) {
                    if (value.item_name === undefined) {
                        value.item_name = "";
                    }
                    let value2 = {};
                    strr += " <tr class=\"gradeX\">\n" +
                        "                                        <td>" + (index + 1) + "</td>\n" +
                        "                                        <td>" + value.item_name + "</td>\n" +
                        "                                        <td>" + value.count + "</td>\n" +
                        "</tr>";
                    value2.value = value.count;
                    value2.name = value.item_name;
                    charts_value.push(value2)
                });

                $("#show14").html(strr);

            } else if (status2 === "edu") {
                strr = "";
                charts_value = [];
                $.each(data.obj, function (index, value) {
                    if (value.item_name === undefined) {
                        value.item_name = "";
                    }
                    let value2 = {};
                    strr += " <tr class=\"gradeX\">\n" +
                        "                                        <td>" + (index + 1) + "</td>\n" +
                        "                                        <td>" + value.item_name + "</td>\n" +
                        "                                        <td>" + value.count + "</td>\n" +
                        "</tr>";
                    value2.value = value.count;
                    value2.name = value.item_name;
                    charts_value.push(value2)
                });

                $("#show14").html(strr);

            } else if (status2 === "degree") {
                strr = "";
                charts_value = [];
                $.each(data.obj, function (index, value) {
                    if (value.item_name === undefined) {
                        value.item_name = "";
                    }
                    let value2 = {};
                    strr += " <tr class=\"gradeX\">\n" +
                        "                                        <td>" + (index + 1) + "</td>\n" +
                        "                                        <td>" + value.item_name + "</td>\n" +
                        "                                        <td>" + value.count + "</td>\n" +
                        "</tr>";
                    value2.value = value.count;
                    value2.name = value.item_name;
                    charts_value.push(value2)
                });

                $("#show14").html(strr);

            }
            else if (status2 === "party_group") {
                strr = "";
                charts_value = [];
                $.each(data.obj, function (index, value) {
                    if (value.item_name === undefined) {
                        value.item_name = "";
                    }
                    let value2 = {};
                    strr += " <tr class=\"gradeX\">\n" +
                        "                                        <td>" + (index + 1) + "</td>\n" +
                        "                                        <td>" + value.item_name + "</td>\n" +
                        "                                        <td>" + value.count + "</td>\n" +
                        "</tr>";
                    value2.value = value.count;
                    value2.name = value.item_name;
                    charts_value.push(value2)
                });

                $("#show14").html(strr);

            }
            //echarts填入数据

            myChart.setOption({
                series: [{
                    data: charts_value
                }]
            })
        }
    });
}
//统计分析
var myChart = echarts.init(document.getElementById('main'));
// 指定图表的配置项和数据
option = {
    title: {
        text: '委员统计',
        // subtext: '纯属虚构',
        x: 'center',
        textStyle: {
            fontSize: 25
        }
    },
    tooltip: {
        trigger: 'item',
        formatter: "{a} <br/>{b} : {c} ({d}%)"
    },
    series: [
        {
            name: '建议状态统计',
            type: 'pie',
            radius: '55%',
            center: ['50%', '60%'],
            itemStyle: {
                emphasis: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
            }
        }
    ]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);